package com.coder.gaeblogger.manager;

import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.coder.gaeblogger.comm.Enums;
import com.coder.gaeblogger.domain.User;

public class UserManager extends BlogManager {
	
	private static final Logger log = Logger.getLogger(UserManager.class.getName());
	
	public User saveUser(User user){		
		super.save(user);
        return user;
		
	}
	
	public User updateUser(User user){
		super.update(user);
        return user;
		
	}
	
	
	public User checkUser(String username, String password){
		PersistenceManager pm = BlogManager.getPMF();
        try {
        	Integer status=Enums.Status.Delete.getId();    		
    		Query query = pm.newQuery("select from " +User.class.getName());
    		query.setFilter("userName == sUserName && password == sPassword && status!= sStatus");  
    		query.declareParameters("String sUserName,String sPassword,Integer sStatus");  	
    		
    		List<?> results = (List<?>) query.execute(username,password,status);
    		if (!results.isEmpty()) {
    			return (User) results.get(0);
    		}
    		
        } finally {
            pm.close();
        }
        return null;
		
	}
	
	
	

}
